package leetcode.code0768;

import java.util.Stack;

import leetcode.IDebug;
import leetcode.helper.H;

//执行用时：0 ms, 在所有 Java 提交中击败了100.00%的用户
//
//内存消耗：41 MB, 在所有 Java 提交中击败了86.45%的用户
//
//通过测试用例：139 / 139
public class Solution3 implements IDebug {

	public int maxChunksToSorted(int[] arr) {
		int len = arr.length, max = -1, p = 0;
		int[] stack = new int[len];
		for (int i = 0; i < len; i++) {
			max = Math.max(max, arr[i]);
			while (p > 0 && arr[i] < stack[p - 1]) {
				p--;
			}
			stack[p++] = max;
		}
		return p;
	}

	@Override
	public void debug4() {
		H.compare(4, this.maxChunksToSorted(H.str2array3("[2,23,4,77,78]")));
		H.compare(1, this.maxChunksToSorted(H.str2array3("[2,1,3,4,4,4,5,2,3,4,5,6,1,2,23,4,5,6,3,3,3,4,5,6]")));//
		H.compare(3, this.maxChunksToSorted(H.str2array3("[2,1,3,4,4,4,5,2,3,4,5,6,1,2,23,4,5,6,3,3,3,4,5,6,77,78]")));//
		H.compare(4, this.maxChunksToSorted(H.str2array3("[2,1,3,4,4]")));
		H.compare(1, this.maxChunksToSorted(H.str2array3("[2,1,3,4,4,4,5,2,3,4,5,6,1,2,23,4,5,6,3,3,3,4,5,6]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution3 so = new Solution3();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
